package com.tenchael.http.interceptor;

import static com.tenchael.http.commons.Constants.LOGIN_USER;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.tenchael.http.domain.User;

@Component
public class AuthInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String uri = request.getRequestURI();
		if (!uri.endsWith("login.do") && !uri.endsWith("logout.do")) {
			String token = request.getHeader("token");
			User userData = (User) request.getSession().getAttribute(LOGIN_USER);
			if (userData == null) {
				response.sendRedirect("login.do");
				return false;
			}
		}
		return true;
	}

}
